package com.zx.cloud.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zx.cloud.ApiVersion;
import com.zx.cloud.Shift;
import com.zx.cloud.common.filter.CurrentUserId;
import com.zx.cloud.common.filter.IgnoreAuth;
import com.zx.cloud.controller.client.ProviderClient;
import com.zx.cloud.controller.client.ResourceClient;
import com.zx.cloud.domain.Resource;
import com.zx.cloud.domain.enums.ResourceStatus;
import com.zx.cloud.model.response.ObjectDataResponse;
import io.swagger.annotations.ApiOperation;


/**
 * 资源
 */
@RestController
@RequestMapping(value = ApiVersion.MANAGER_V1_PREFIX+"/resource", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public class ResourceController{

	private static final Logger logger=Logger.getLogger(ResourceController.class);
	
	@Autowired
    private ProviderClient providerClient;
	
	@Autowired
	private ResourceClient resourceClient;
	
	@ApiOperation(value = "获取左侧菜单资源权限", notes = "获取左侧菜单资源权限")
	@RequestMapping(value = "/leftMenu", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<List<Resource>> leftMenu(){
		return resourceClient.leftMenu();
	}

	@ApiOperation(value = "查询指定资源权限", notes = "查询指定资源权限")
	@RequestMapping(value = "/find", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Object> find() {
	    final ObjectDataResponse<Object> datas = resourceClient.findAllSource();
	    if (datas == null) {
	    	Shift.fatal(StatusCode.USER_NOT_EXISTS);
	    }
	    return datas;
	}
	
	@ApiOperation(value = "更新资源权限", notes = "更新资源权限")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Object> update(@CurrentUserId Long userId,@RequestBody  Resource resource){
		 final ObjectDataResponse<Object> datas = resourceClient.update(resource);
		    if (datas == null) {
		    	Shift.fatal(StatusCode.USER_NOT_EXISTS);
		    }
		    return datas;
	}
	
	@ApiOperation(value = "添加资源权限", notes = "添加资源权限")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Object> add(@CurrentUserId Long userId,@RequestBody  Resource resource){
		final ObjectDataResponse<Object> datas = resourceClient.insert(resource);
	    if (datas == null) {
	    	Shift.fatal(StatusCode.USER_NOT_EXISTS);
	    }
	    return datas;
	}
	
	@ApiOperation(value = "删除资源权限", notes = "删除资源权限")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Object> delete(@CurrentUserId Long userId,@RequestBody  Resource resource){
		final ObjectDataResponse<Object> datas = resourceClient.delete(resource);
	    if (datas == null) {
	    	Shift.fatal(StatusCode.USER_NOT_EXISTS);
	    }
	    return datas;
	}
}
